package hu.fic.weddingstorefinder.db.generatedtable;

import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import hu.fic.weddingstorefinder.db.Table;

public class GeneratedApplicationConf extends Table {
	private static final String LOG_TAG = "GeneratedApplicationConf";

	public static final String TAB_NAME = "ApplicationConf";

	//variables for AppConfId
	public static final String COL_APPCONFID = "AppConfId";
	public static final int INT_COL_APPCONFID = 0;
	protected Integer filter_APPCONFID = null;

	//variables for Name
	public static final String COL_NAME = "Name";
	public static final int INT_COL_NAME = 1;
	protected String filter_NAME = null;
	protected String set_NAME = null;
	protected boolean forced_NAME = false;

	//variables for Value
	public static final String COL_VALUE = "Value";
	public static final int INT_COL_VALUE = 2;
	protected String filter_VALUE = null;
	protected String set_VALUE = null;
	protected boolean forced_VALUE = false;

	private boolean loaded = false;

	//constructor
	public GeneratedApplicationConf(SQLiteDatabase db, Activity activity) {
		super(db, activity);
	}

	//methods for AppConfId
	public void setAppConfId(int p) {
		this.filter_APPCONFID = new Integer(p);
	}

	public int getAppConfId() {
		if (this.loaded) {
			return this.cursor.getInt(INT_COL_APPCONFID);
		} else {
			return this.filter_APPCONFID.intValue();
		}
	}

	//methods for Name
	public void setName(String p) {
		this.filter_NAME = p;
	}

	public String getName() {
		if (this.loaded) {
			return this.cursor.getString(INT_COL_NAME);
		} else {
			return this.filter_NAME;
		}
	}
	public void addNameUpdate(String p) throws Exception {
		if (!this.loaded) {
			this.set_NAME = p;
		} else {
			throw new Exception("The addNameUpdate() cannot be used after load.");
		}
	}
	public void forceNameColumn() throws Exception {
		if (!this.loaded) {
			this.forced_NAME = true;
		} else {
			throw new Exception("The forcedNameColumn() cannot be used after load.");
		}
	}

	//methods for Value
	public void setValue(String p) {
		this.filter_VALUE = p;
	}

	public String getValue() {
		if (this.loaded) {
			return this.cursor.getString(INT_COL_VALUE);
		} else {
			return this.filter_VALUE;
		}
	}
	public void addValueUpdate(String p) throws Exception {
		if (!this.loaded) {
			this.set_VALUE = p;
		} else {
			throw new Exception("The addValueUpdate() cannot be used after load.");
		}
	}
	public void forceValueColumn() throws Exception {
		if (!this.loaded) {
			this.forced_VALUE = true;
		} else {
			throw new Exception("The forcedValueColumn() cannot be used after load.");
		}
	}

	public void loadAll() throws Exception {
		load();
	}

	public void load(String sql) throws Exception {
		this.loaded = true;
		super.load(sql);
	}

	public void load() throws Exception {
		SQLStatement sql = new SQLStatement();
		sql.addTable(TAB_NAME);

		sql.addIntFilter(COL_APPCONFID, this.filter_APPCONFID);
		if (!this.forced_NAME || this.filter_NAME != null) {
			sql.addStrFilter(COL_NAME, this.filter_NAME);
		} else {
			sql.addNullFilter(COL_NAME);
		}
		if (!this.forced_VALUE || this.filter_VALUE != null) {
			sql.addStrFilter(COL_VALUE, this.filter_VALUE);
		} else {
			sql.addNullFilter(COL_VALUE);
		}

		sql.addTail(" ORDER BY " + COL_APPCONFID);
		load(sql.getSql());
	}

	public void clear() {
		this.filter_APPCONFID = null;

		this.filter_NAME = null;
		this.set_NAME = null;
		this.forced_NAME = false;

		this.filter_VALUE = null;
		this.set_VALUE = null;
		this.forced_VALUE = false;

		this.loaded = false;
		super.clear();
	}

	public long newRow() throws Exception {
		try {
			ContentValues newTaskValues = new ContentValues();
			newTaskValues.put(COL_NAME, filter_NAME);
			newTaskValues.put(COL_VALUE, filter_VALUE);
			return this.db.insert(TAB_NAME, null, newTaskValues);
		} catch (Exception e) {
			Log.e(LOG_TAG, "Cannot execute inserting (" + e.getMessage() + ")", e);
			throw new Exception("Insert failed, see log file for details!");
		}
	}

	public void delete() throws Exception {
		SQLStatement sql = new SQLStatement();
		sql.addTable(TAB_NAME);
		sql.addIntFilter(COL_APPCONFID, this.filter_APPCONFID);
		sql.addStrFilter(COL_NAME, this.filter_NAME);
		sql.addStrFilter(COL_VALUE, this.filter_VALUE);
		this.db.execSQL(sql.getDeleteSql());
	}

	public void deleteAllRows() throws Exception {
		SQLStatement sql = new SQLStatement();
		sql.addTable(TAB_NAME);
		this.db.execSQL(sql.getDeleteAllSql());
	}

	public void update() throws Exception {
		SQLStatement sql = new SQLStatement();
		sql.addTable(TAB_NAME);

		sql.addIntFilter(COL_APPCONFID, this.filter_APPCONFID);

		sql.addStrFilter(COL_NAME, this.filter_NAME);
		sql.addStringSet(COL_NAME, this.set_NAME);

		sql.addStrFilter(COL_VALUE, this.filter_VALUE);
		sql.addStringSet(COL_VALUE, this.set_VALUE);

		this.db.execSQL(sql.getUpdateSql());
	}

}